/**
 * 数据请求
 */
import http from '@ohos.net.http';
import { ChatResponse } from '../common/bean/ChatResponse';
import { Constants } from '../common/Constants';
import { Log } from '../common/Log';
class HttpRequest {
  instance: http.HttpRequest;

  constructor() {
    this.instance = http.createHttp();
  }

  post = (appid: string, chatId: string, content: string): Promise<ChatResponse> => {

    Log.info(Constants.LOG_TAG_NAME, `chat request: ${Constants.CHAT_SERVER_URL}  ${chatId} ${content}`);
    return new Promise((resolve, reject) => {
      this.instance.request(`${Constants.CHAT_SERVER_URL}`, {
        method: http.RequestMethod.POST,
        header: {
          'Content-Type': 'application/json',
          'Authorization': Constants.CHAT_AUTHORIZATION + appid
        },
        extraData: {
          "chatId": chatId,
          "stream": false,
          "messages": [
            {
              "content": content,
              "role": "user"
            }
          ]
        },
        expectDataType: http.HttpDataType.STRING
      }).then((ret) => {
        Log.info(Constants.LOG_TAG_NAME, `========chatResponsed request: ${JSON.stringify(ret)}`);
        Log.info(Constants.LOG_TAG_NAME, `========chatResponsed request: ${JSON.stringify(ret)}`);
        Log.info(Constants.LOG_TAG_NAME, `========chatResponsed request: ${JSON.stringify(ret)}`);
        Log.info(Constants.LOG_TAG_NAME, `========chatResponsed request: ${JSON.stringify(ret)}`);
        Log.info(Constants.LOG_TAG_NAME, `========chatResponsed request: ${JSON.stringify(ret)}`);
        Log.info(Constants.LOG_TAG_NAME, `========chatResponsed request: ${JSON.stringify(ret)}`);

        if (ret.responseCode === http.ResponseCode.OK) {
          Log.info(Constants.LOG_TAG_NAME, `========chatok request: ${JSON.stringify(ret.result)}`);
          Log.info(Constants.LOG_TAG_NAME, `========chatok request: ${JSON.stringify(ret.result)}`);
          Log.info(Constants.LOG_TAG_NAME, `========chatok request: ${JSON.stringify(ret.result)}`);
          Log.info(Constants.LOG_TAG_NAME, `========chatok request: ${JSON.stringify(ret.result)}`);
          Log.info(Constants.LOG_TAG_NAME, `========chatok request: ${JSON.stringify(ret.result)}`);
          Log.info(Constants.LOG_TAG_NAME, `========chatok request: ${JSON.stringify(ret.result)}`);


          let result = JSON.parse(ret.result as string);
          let newChatId = result.newChatId;
          let choice = result['choices'][0];

          let content = choice['message']['content'];
          resolve(new ChatResponse(newChatId, content));
        } else {
          let result = JSON.parse(ret.result as string);
          resolve(new ChatResponse("", result.message));
        }
      }).catch((err) => {
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        Log.error(Constants.LOG_TAG_NAME, `===chat faild: ${JSON.stringify(err)}`);
        reject(new ChatResponse("", "服务器开小差了，请稍后尝试！"));
      })
    });
  }
}

export const HttpUtil = new HttpRequest();